package com.agileboot.medicine.controller.system;

import com.agileboot.common.core.dto.ResponseDTO;
import com.agileboot.domain.medicine.vo.MedicineVO;
import com.agileboot.domain.system.user.UserApplicationService;
import com.agileboot.domain.system.user.command.UpdateProfileCommand;
import com.agileboot.domain.system.user.command.UpdateUserAvatarCommand;
import com.agileboot.domain.system.user.db.SysUserEntity;
import com.agileboot.domain.system.user.dto.UserDTO;
import com.agileboot.infrastructure.user.AuthenticationUtils;
import com.agileboot.infrastructure.user.web.SystemLoginUser;
import io.swagger.v3.oas.annotations.Operation;
import org.apache.http.util.Asserts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;




@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserApplicationService userApplicationService;
    /**
     * 修改用户信息
     */
    @Operation(summary = "修改用户信息")
    @PostMapping("/edit")
    public ResponseDTO editUserInfo(@RequestBody UpdateProfileCommand command) {
        SystemLoginUser loginUser = AuthenticationUtils.getSystemLoginUser();
        command.setUserId(loginUser.getUserId());
        userApplicationService.updateUserProfile(command);
        return ResponseDTO.ok();
    }

    /**
     * 修改用户头像
     */
    @Operation(summary = "修改用户头像")
    @GetMapping("/edit/avatar")
    public ResponseDTO editUserInfo(String avatar) {
        Asserts.notNull(avatar,"头像地址不能为空");
        SystemLoginUser loginUser = AuthenticationUtils.getSystemLoginUser();
        Asserts.notNull(loginUser,"该用户没有登录");
        SysUserEntity entity = new SysUserEntity();
        entity.setAvatar(avatar);
        entity.setUserId(loginUser.getUserId());
        userApplicationService.updateAvatar(entity);
        return ResponseDTO.ok();
    }

    /**
     * 获取用户信息
     */
    @Operation(summary = "获取用户信息")
    @GetMapping("/info")
    public ResponseDTO<UserDTO> getUserInfo() {
        SystemLoginUser loginUser = AuthenticationUtils.getSystemLoginUser();
        UserDTO userDTO = userApplicationService.getUserInfo(loginUser.getUserId());
        return ResponseDTO.ok(userDTO);
    }
}
